package com.ytlz.dmcs.model.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.ytlz.dmcs.model.system.dao.SysFieldDao;
import com.ytlz.dmcs.model.system.dao.SysMenuDao;
import com.ytlz.dmcs.model.system.dao.SysObjectDao;
import com.ytlz.dmcs.model.system.entity.SysField;
import com.ytlz.dmcs.model.system.entity.SysMenu;
import com.ytlz.dmcs.model.system.entity.SysObject;
import com.ytlz.dmcs.model.system.service.SysObjectService;

@Service
public class SysObjectServiceImpl implements SysObjectService{

	@Autowired
	private SysObjectDao sysObjectDao;
	
	@Autowired
	private SysFieldDao sysFieldDao;
	
	@Autowired
	private SysMenuDao sysMenuDao;
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Override
	public SysObject save(SysObject obj) {
		if(sysObjectDao.findByName(obj.getName())==null) {
			SysObject data = sysObjectDao.save(obj);
			List<SysField> fields = new ArrayList<SysField>();
			SysField field = new SysField("name","名称","text","system",obj);
			field=sysFieldDao.save(field);
			fields.add(field);
			
			field = new SysField("create_date","创建时间","date","system",obj);
			field=sysFieldDao.save(field);
			fields.add(field);
			
			field = new SysField("modify_date","编辑时间","date","system",obj);
			field=sysFieldDao.save(field);
			fields.add(field);
			
			data.setFields(fields);
			data = sysObjectDao.save(data);	
			
			Iterator<SysMenu> menus = sysMenuDao.findAll().iterator();
			SysMenu parentMenu = null;
			while (menus.hasNext()) {
				SysMenu sysMenu = menus.next();
				if(sysMenu.getText().equals("数据管理")) {
					parentMenu = sysMenu;
				}
				
			}
			if(parentMenu!=null) {				
				SysMenu submenu = new SysMenu(data.getLabel(), "datalist?type="+data.getName() , parentMenu.getId(), "custom");
				submenu.setDataType(obj.getName());
				submenu = sysMenuDao.save(submenu);
			}
			
			return data;
		}else {
			return null;
		}
	}
	
	

	@Override
	public List<SysField> getFields(SysObject obj, String scope) {
		return sysFieldDao.findByParentObjAndScope(obj, scope);
	}

	@Override
	public List<SysObject> findAll() {
		Iterator<SysObject> result = sysObjectDao.findAll().iterator();
		List<SysObject> resultList = new ArrayList<SysObject>();
		while (result.hasNext()) {
			SysObject sysObject = (SysObject) result.next();
			resultList.add(sysObject);
		}
		return resultList;
	}



	@Override
	public SysObject get(String id) {
		SysObject obj = sysObjectDao.findById(id).get();
		return obj;
	}



	@Override
	public SysObject findByName(String name) {
		return sysObjectDao.findByName(name);
	}



	@Override
	public void remove(String id) {
		SysObject entity = sysObjectDao.findById(id).get();
		entity.getName();
		
		for (SysField field : entity.getFields()) {
			sysFieldDao.delete(field);
		}
		
		
		List<SysMenu> menuList = sysMenuDao.findByDataType(entity.getName());
		for (SysMenu sysMenu : menuList) {
			sysMenuDao.delete(sysMenu);
		}
		
		if(mongoTemplate.collectionExists(entity.getName())) {
			mongoTemplate.dropCollection(entity.getName());
		}		
		
		sysObjectDao.delete(entity);
		
	}

	
}
